package com.example.bbs.controller;

import com.example.bbs.bean.Bbstype;
import com.example.bbs.bean.Note;
import com.example.bbs.bean.User;
import com.example.bbs.http.bean.HttpSendBasic;
import com.example.bbs.http.bean.file.UploadFile;
import com.example.bbs.http.bean.type.HomePageData;
import com.example.bbs.http.bean.user.UserLoginInfo;
import com.example.bbs.http.bean.user.UserShowSimple;
import com.example.bbs.service.NoteService;
import com.example.bbs.service.TypeService;
import com.example.bbs.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/api/user")
@Api(tags = {"用户接口"})

public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private NoteService noteService;

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public String register(HttpServletRequest request, @RequestParam("name") String name, @RequestParam("pass") String pass,
                           Model model) {
        HttpSendBasic basic = userService.register(name, pass);
//        userLogin(request, name, pass);
        return userLogin(request, name, pass, model);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public String userLogin(HttpServletRequest request, @RequestParam("name") String name, @RequestParam("pass") String pass,
                                   Model model) {
        User user = userService.login(name, pass);
        if (user == null) {
            HttpSendBasic httpSendBasic = new HttpSendBasic();
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("账号或密码错误");
            model.addAttribute("message", httpSendBasic.getMessage());
            return "login";
        }
        request.getSession().setAttribute("user", user);
//        HttpSendBasic httpSendBasic = new HttpSendBasic();
//        httpSendBasic.setSuccess(1);
//        httpSendBasic.setMessage("ok");
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        model.addAttribute("user", user);
        model.addAttribute("message", "");
        return "type";
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation("修改用户信息")
    @ResponseBody
    public HttpSendBasic updateUserInfo(@RequestParam(value = "password", required = false) String password
            , @RequestParam(value = "nickname", required = false) String nickname, @RequestParam(value = "headimg", required = false) String headimg, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return userService.updateUserInfo(user.getUserid(), password, nickname, headimg);

    }


    @GetMapping("/statue")
    @ApiOperation("用户登录状态")
    public UserLoginInfo userStatue(HttpServletRequest request) {
        UserLoginInfo httpSendBasic = new UserLoginInfo();
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            httpSendBasic.setMessage("ok");
            httpSendBasic.setSuccess(1);
            UserShowSimple userShowSimple = new UserShowSimple();
            userShowSimple.setNickname(user.getNickname());
            userShowSimple.setHeadimage(user.getHeadimage());
            userShowSimple.setUserid(user.getUserid());
            httpSendBasic.setUser(userShowSimple);
            return httpSendBasic;
        }
        httpSendBasic.setMessage("未登录");
        httpSendBasic.setSuccess(0);
        return httpSendBasic;
    }

    @GetMapping("/info")
    @ApiOperation("用户登录状态")
    public UserLoginInfo userInfo(@RequestParam("userid") Integer userid) {
        return userService.info(userid);
    }

    @PostMapping("/file")
    @ApiOperation("用户上传文件")
    @ResponseBody
    public UploadFile user_uploadFile(@RequestParam("file") MultipartFile file) {
        UploadFile uploadFile = new UploadFile();
        try {
            String s = userService.uploadFile(file);
            uploadFile.setMessage("ok");
            uploadFile.setUrl(s);
            uploadFile.setSuccess(1);
        } catch (IOException e) {
            e.printStackTrace();
            uploadFile.setMessage("IO错误");
            uploadFile.setSuccess(0);
        }
        return uploadFile;
    }

    @GetMapping("/note/toCreate")
    //跳转到发帖页面
    public String toCreate(Model model){
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return "createNote";
    }

    @PostMapping("/note/getType")
    @ResponseBody
    //通过tagid获取分类
    public List<Bbstype> getType(@RequestParam("tagId") Integer tagId){
        List<Bbstype> types = noteService.selectTypeByTagid(tagId);
        return types;
    }

    @PostMapping("/note/create")
    @ApiOperation("用户发帖")
    @ResponseBody
    public HttpSendBasic sendNote(@RequestParam("typeid") Integer typeid, @RequestParam("tittle") String noteName, @RequestParam("text") String note, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return userService.sendNote(typeid, noteName, note, user);
    }


    @PostMapping("/note/edit")
    @ApiOperation("用户编辑帖")
    @ResponseBody
    public HttpSendBasic editNote(@RequestParam(value = "noteid") Integer noteId, @RequestParam(value = "typeid", required = false) Integer typeid, @RequestParam(value = "tittle", required = false) String noteName, @RequestParam(value = "text", required = false) String note, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return userService.editNote(noteId, typeid, noteName, note, user);
    }

    @PostMapping("/commemt/create")
    @ApiOperation("评论")
    @ResponseBody
    public HttpSendBasic sendComment(@RequestParam(value = "noteid") Integer noteId, @RequestParam(value = "text") String text, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return userService.sendComment(noteId, text, user);
    }

    @PostMapping("/commemt/edit")
    @ApiOperation("编辑评论")
    @ResponseBody
    public HttpSendBasic editComment(@RequestParam(value = "commentid") Integer commentId, @RequestParam(value = "text") String text, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return userService.editComment(commentId, text, user);
    }

}
